﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using Newtonsoft.Json;

namespace JacketEmblem.Crane.UnitTests.NUnitExtensions
{
    public class JsonTestDataFileReader<T> : DataFileReader where T : DataFileTestInfo
    {
        private List<DataFileTestInfo> tests = new List<DataFileTestInfo>();

        public override ReadOnlyCollection<DataFileTestInfo> Tests
        {
            get { return tests.AsReadOnly(); }
        }

        public override void ReadDataFile(string fileName)
        {
            string fileContents = File.ReadAllText(fileName);
            DataFileTestInfo[] testWrapper = JsonConvert.DeserializeObject<DataFileTestInfo[]>(fileContents, new TestDataFileJsonConverter<T>());
            foreach (DataFileTestInfo info in testWrapper)
            {
                tests.Add(info);
            }
        }
    }
}
